package com.teamin.web.commandlinerun;

import com.teamin.service.FileService;
import com.teamin.service.impl.wx.WeChatMPTokenManager;
import com.teamin.service.impl.wx.WeChatSmallProgramTokenManager;
import com.teamin.web.Evn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * Created by qfu on 2017/9/19.
 * 当服务启动 时运行
 */
@Component
public class InitCommandLine implements CommandLineRunner {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${wxmp.appid}")
    public String APP_ID ;

    @Value("${wxmp.secret}")
    public String SECRET ;

    @Value("${wxmp.sendurl}")	//微信小程序发送消息接口
    String WX_SERVER_SENDMSG_API ;

    @Value("${wxmp.send_card_url}")	//微信公众号发送消息接口
    String WX_SERVER_MP_SENDMSG_API ;

    @Value("${wx.appid}")	//小程序APPID
            String SMALL_APP_ID ;

    @Value("${wx.secret}")	//小程序的密钥
            String SMALL_APP_SECRET ;

    @Value("${spring.profiles.active}")	//服务器环境
            String SERVICE_PROFILS ;
    @Value("${wx.send_card_url}")	//服务器环境
            String SERVICE_URL_SMALLCARD ;

    //OSS 结点名
    @Value("${oss.buckname}")	//OSS KEY
            String ALIYUN_OSS_BUCKENAME ;
    @Value("${oss.appid}")
    String ALIYUN_OSS_KEYID ;
    //OSS 密钥
    @Value("${oss.secret}")
    String ALIYUM_OSS_KEYSECRET ;
    //OSS 存储的地域
    @Value("${oss.endpoint}")
    String ALIYUN_OSS_ENDPOINT ;
    //创建的URL的过期时间
    @Value("${oss.expires}")
    Long ALIYUN_OSS_EXPIRES ;

    @Autowired
    FileService fileService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(">>>>>>>>>>>>>>>InitCommandLine<<<<<<<<<<<<<");

        Evn.APP_ID = APP_ID ;
        Evn.SECRET = SECRET ;
        Evn.SMALL_APP_ID = SMALL_APP_ID ;
        Evn.SMALL_APP_SECRET = SMALL_APP_SECRET ;

        Evn.WX_SERVER_SENDMSG_API= WX_SERVER_SENDMSG_API;
        Evn.WX_SERVER_MP_SENDMSG_API= WX_SERVER_MP_SENDMSG_API;
        Evn.SERVICE_PROFILS = SERVICE_PROFILS ;

        //初使化支持的缩略图格式
        FileService.suportThumbnail.add("jpg") ;
        FileService.suportThumbnail.add("png") ;
        FileService.suportThumbnail.add("jpeg") ;

        //OSS
        Evn.ALIYUN_OSS_BUCKENAME=ALIYUN_OSS_BUCKENAME ;
        Evn.ALIYUN_OSS_KEYID=ALIYUN_OSS_KEYID ;
        Evn.ALIYUM_OSS_KEYSECRET=ALIYUM_OSS_KEYSECRET ;
        Evn.ALIYUN_OSS_ENDPOINT=ALIYUN_OSS_ENDPOINT ;
        Evn.ALIYUN_OSS_EXPIRES =ALIYUN_OSS_EXPIRES;


        Evn.SERVICE_URL_SMALLCARD = SERVICE_URL_SMALLCARD ;

        //小程序间隔性的获取token
        WeChatSmallProgramTokenManager.getInstance();

        //公众号间隔性的获取token
        WeChatMPTokenManager.getInstance();
    }
}
